package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"user-app/models"
)

type UserAppController struct {
	beego.Controller
}

func (c *UserAppController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	var count int64
	models.DB.Model(&models.UserApp{}).Count(&count)
	sumpage := int(math.Ceil(float64(count) / float64(size)))
	if page > sumpage {
		page = sumpage
	}
	offset := (page - 1) * size
	var user []models.UserApp
	err := models.DB.Offset(offset).Limit(size).Find(&user).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "查询失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"msg":     "查询成功",
		"data":    user,
		"page":    page,
		"size":    size,
		"count":   count,
		"sumpage": sumpage,
	}
	c.ServeJSON()
}
